/**
 * Copyright 2011, Under-Ground Studio.
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.edu.zju.vlis.aus.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import cn.edu.zju.vlis.aus.MainActivity;
import cn.edu.zju.vlis.aus.R;
import cn.edu.zju.vlis.aus.chart.BarChartAdapter;
import cn.edu.zju.vlis.aus.chart.BarChartView;
import cn.edu.zju.vlis.aus.chart.PieChartAdapter;
import cn.edu.zju.vlis.aus.chart.PieChartView;
import cn.edu.zju.vlis.aus.db.UsageRecordDAO;

public class ChartActivity extends Activity {
	// two chart view
	private LayoutInflater inflater;
	private View piechartLayout;
	private PieChartView pieChart;
	private Button preBtn;
	private Button nxtBtn;
	private PieChartAdapter pieAdapter;
	private BarChartView barChart;
	private BarChartAdapter barAdapter;
	private UsageRecordDAO dao;
	private int curItem;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		inflater = LayoutInflater.from(this);
		dao = new UsageRecordDAO(this);
		ViewPager pager = new ViewPager(this);
		pager.setOnPageChangeListener(new PageChangeListener());
		pager.setAdapter(new ChartPagerAdapter());
		setContentView(pager);
	}

	@Override
	protected void onDestroy() {
		dao.close();
		super.onDestroy();
	}

	/**
	 * refresh this activity to take effect data change.
	 */
	public void refresh() {
		if (pieChart != null) {
			if (pieAdapter != null) {
				pieAdapter.removeListener(pieChart);
			}
			pieAdapter = new PieChartAdapter(this, dao);
			pieChart.setAdapter(pieAdapter);
			pieAdapter.fireDataChangeEvent();
		}
		if (barChart != null) {
			barAdapter = new BarChartAdapter(this, dao);
			barChart.setAdapter(barAdapter);
			barAdapter.notifyDataSetChanged();
		}
	}
	
	public int getCurrentItem(){
		return curItem;
	}

	private class ChartPagerAdapter extends PagerAdapter {
		@Override
		public int getCount() {
			return 2;
		}

		@Override
		public Object instantiateItem(View container, int position) {
			ViewPager pager = (ViewPager) container;
			View view = null;
			switch (position) {
			case 0:
				piechartLayout = inflater.inflate(R.layout.pie_chart_layout,
						null);
				pieChart = (PieChartView) piechartLayout.findViewById(R.id.pie_chart_view);
				preBtn = (Button) piechartLayout.findViewById(R.id.pie_chart_button_pre);
				preBtn.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						pieChart.selectPrev();
					}
				});
				nxtBtn = (Button) piechartLayout.findViewById(R.id.pie_chart_button_nxt);
				nxtBtn.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						pieChart.selectNext();
					}
				});
				pieAdapter = new PieChartAdapter(ChartActivity.this, dao);
				pieChart.setAdapter(pieAdapter);
				pager.addView(piechartLayout);
				view = piechartLayout;
				break;
			case 1:
				if (barChart == null) {
					barChart = new BarChartView(ChartActivity.this);
					barAdapter = new BarChartAdapter(ChartActivity.this, dao);
					barChart.setAdapter(barAdapter);
				}
				pager.addView(barChart);
				view = barChart;
				break;
			}
			return view;
		}

		@Override
		public void destroyItem(View container, int position, Object object) {
			ViewPager pager = (ViewPager) container;
			pager.removeViewAt(position);
		}

		@Override
		public void startUpdate(View container) {
		}

		@Override
		public void finishUpdate(View container) {
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return view == object;
		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public void restoreState(Parcelable state, ClassLoader loader) {
		}
	}

	/**
	 * @author ymsong (songyuming1985@gmail.com) 2011-9-25
	 */
	public class PageChangeListener implements OnPageChangeListener {

		@Override
		public void onPageScrolled(int position, float positionOffset,
				int positionOffsetPixels) {
		}

		@Override
		public void onPageSelected(int position) {
			curItem = position;
			String[] charts = getResources().getStringArray(R.array.title_statistical_chart);
			MainActivity mainActivity = (MainActivity) getParent();
			mainActivity.setTitle(charts[curItem]);
		}

		@Override
		public void onPageScrollStateChanged(int state) {
			// do nothing.
		}

	}
}
